package spec.chenli;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

//@WebServlet(name = "ChaxunServlet")
public class ChaxunServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = null;
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/1613580007_1?characterEncoding=utf8", "root",  "123456789");
            String sql="select * FROM  t_news WHERE id=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,request.getParameter("id"));
            ResultSet rs=ps.executeQuery();
            out.println("<html><head>");
            out.println("<meta http-equiv='content-type' content='text/html;charset=utf-8'></head>");
          //  Integer id = Integer.parseInt(request.getParameter("id"));
            out.println("<table border='1' align='center' width=’1000px' hegin='1200px' cellpadding='1' cellspacing='1'>");
            out.println("<caption><h1>新闻列表</h1></caption>");
            while(rs.next()) {
                String id= rs.getString("id");
                String title = rs.getString("title");
                String content = rs.getString("content");
                String publisher = rs.getString("publisher");
                String publish_time = rs.getString("publish_time");
                out.println("<tr>");
                out.println("<td>序号:<td>" + id + "</td></td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>标题:<td>" + title + "</td></td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>内容:<td>" + content + "</td></td>");
                out.println("</tr>");


                out.println("<tr>");
                out.println("<td>发布人:<td>" + publish_time + "</td></td>");
                out.println("</tr>");

                out.println("<tr>");
                out.println("<td>发布人:<td>" + publisher + "</td></td>");
                out.println("</tr>");

            }
                out.println("</table>");
                out.println("<p></p>");
                out.println("<p></p>");
                out.println("<p></p>");
                out.println("<tr><td><center><input type='button' style='width: 80px;height:50px' value='返回'onClick='location.href=\"http://localhost:8080/listnew\"'/></center></td></tr>");
                out.println("</form></html>");
            con.close();
            con.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

